<template>
	<view class="banner_detail">
		<view class="door">
			<image src="/static/index/food.png" mode=""></image>
			<view class="door_name">
				老成都担担面
			</view>
			<view class="door_sale">
				月售238
			</view>
			<view class="address">
				学府路218号柳城街道
			</view>
		</view>
	<view class="u-wrap">
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
				 :data-current="index" @tap.stop="swichMenu(index)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<block v-for="(item,index) in tabbar" :key="index">
				<scroll-view scroll-y class="right-box" v-if="current==index">
					<view class="page-view">
						<view class="class-item">
							<view class="item-title">
								<text>{{item.name}}</text>
							</view>
							<view class="item-container">
								<view class="thumb-box" v-for="(item1, index1) in item.foods" :key="index1">
									<!-- <image class="item-menu-image" :src="item1.icon" mode=""></image>
									<view class="item-menu-name">{{item1.name}}</view> -->
									<view class="food_img">
										<image class="item-menu-image" :src="item1.icon" mode=""></image>
									</view>
									<view class="food_des">
										<view class="name">
											{{item1.name}}
										</view>
										<view class="sale">
											销量：20
										</view>
										<view class="price">
											￥9
										</view>
									</view>
									<view class="add_food">
										<view class="plus" @tap="add($event,item1.name)">
											+
										</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
	</view>
	<!-- 购物车popup窗 -->
	<u-popup :show="showCart" @close="showCart = false" @open="open" round="5">
	            <view class="details">
	                <view class="top">
	                	<view class="choose">
							所选商品
						</view>
						<view class="clear">
							清空
						</view>
	                </view>
					<view class="content">
						<view class="item_choose" v-for="item,index in 2" :key="index" >
							<view class="name">
								花甲米线
							</view>
							<view class="numbox">
								<u-number-box :min="1" :max="99"></u-number-box>
							</view>
							<view class="price">
								￥11
							</view>
						</view>
					</view>
					<view class="next" @click="goPay">
						下一步
					</view>
	            </view>
			</u-popup>
			<view class="foot">
				<view class="cart" @click="showCart = true">
					购物车({{foodCount}})
					<image id="cart" src="../../static/cart.png" mode="widthFix"></image>
				</view>
				<view class="next" @click="goPay">
					下一步
				</view>
			</view>
	<!-- 只需要绑定购物车位置即可 -->
	<flyInCart ref="inCart" :cartBasketRect="cartBasketRect"></flyInCart>
	</view>
</template>

<script>
	import classifyData from "@/common/classify.data.js";
	import flyInCart from '@/components/flyInCart.vue'
	export default {
		components:{
			flyInCart
		},
		data() {
			return {
				foodCount:0, //购物车数量
				showCart:false, //
				cartBasketRect:{},//购物车位置数据
				tabbar: classifyData,
				scrollTop: 0, //tab标题的滚动条位置
				current: 0, // 预设当前项的值
				menuHeight: 0, // 左边菜单的高度
				menuItemHeight: 0, // 左边菜单item的高度
			}
		},
		computed: {
			
		},
		onPageScroll() {
			//调用是因为 重复使用了点击动画位置 保证不抖动。 不传参调用即清空点击点
			this.$refs.inCart.addToCart();
		},
		onReady() {
			const self = this
		 
			let q = uni.createSelectorQuery()
			//获取购物车位置
			setTimeout(function(){
				q.select('#cart').boundingClientRect(data => {
				 self.cartBasketRect = data
				}).exec();
			},100)
		},
		methods: {
			add(e,id){
				this.$refs.inCart.addToCart(e,id);
				this.foodCount++
				// this.placeState = true
			},
			open() {
				
			},
			getImg() {
				return Math.floor(Math.random() * 35);
			},
			// 点击左边的栏目切换
			async swichMenu(index) {
				if(index == this.current) return ;
				this.current = index;
				// 如果为0，意味着尚未初始化
				if(this.menuHeight == 0 || this.menuItemHeight == 0) {
					await this.getElRect('menu-scroll-view', 'menuHeight');
					await this.getElRect('u-tab-item', 'menuItemHeight');
				}
				// 将菜单菜单活动item垂直居中
				this.scrollTop = index * this.menuItemHeight + this.menuItemHeight / 2 - this.menuHeight / 2;
			},
			// 获取一个目标元素的高度
			getElRect(elClass, dataVal) {
				new Promise((resolve, reject) => {
					const query = uni.createSelectorQuery().in(this);
					query.select('.' + elClass).fields({size: true},res => {
						// 如果节点尚未生成，res值为null，循环调用执行
						if(!res) {
							setTimeout(() => {
								this.getElRect(elClass);
							}, 10);
							return ;
						}
						this[dataVal] = res.height;
					}).exec();
				})
			},
			goPay() {
				uni.navigateTo({
					url:'/SubPages/settlement/settlement'
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.banner_detail {
		height: 30vh;
		background: linear-gradient( #F8C271,#fff);
		padding-top:1rpx ;
		.door {
			text-align: center;
			padding: 20rpx 0;
			margin: 25rpx;
			background-color: #fff;
			border-radius: 15rpx;
			image {
				height: 150rpx;
				width: 150rpx;
				border-radius: 10rpx;
				margin: 10rpx 0;
			}
			.door_name {
				font-weight: 700;
			}
			.address {
				font-size: 28rpx;
				color: #aaa;
			}
			.door_sale {
				font-size: 28rpx;
				color: #aaa;
				margin: 10rpx 0;
			}
		}
	}
	.u-wrap {
		height: calc(100vh);
		/* #ifdef H5 */
		height: calc(100vh - var(--window-top));
		/* #endif */
		display: flex;
		flex-direction: column;
	}

	.u-search-box {
		padding: 18rpx 30rpx;
	}

	.u-menu-wrap {
		flex: 1;
		display: flex;
		overflow: hidden;
	}

	.u-search-inner {
		background-color: rgb(234, 234, 234);
		border-radius: 100rpx;
		display: flex;
		align-items: center;
		padding: 10rpx 16rpx;
	}

	.u-search-text {
		font-size: 26rpx;
		color: $u-tips-color;
		margin-left: 10rpx;
	}

	.u-tab-view {
		width: 200rpx;
		height: 100%;
	}

	.u-tab-item {
		height: 110rpx;
		background: #f6f6f6;
		box-sizing: border-box;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 26rpx;
		color: #444;
		font-weight: 400;
		line-height: 1;
	}
	
	.u-tab-item-active {
		position: relative;
		color: #000;
		font-size: 30rpx;
		font-weight: 600;
		background: #fff;
	}
	
	.u-tab-item-active::before {
		content: "";
		position: absolute;
		border-left: 4px solid #F5AC3C;
		height: 32rpx;
		left: 0;
		top: 39rpx;
	}

	.u-tab-view {
		height: 100%;
	}
	
	.right-box {
		background-color: rgb(250, 250, 250);
	}
	
	.page-view {
		padding: 16rpx;
	}
	
	.class-item {
		margin-bottom: 30rpx;
		background-color: #fff;
		padding: 16rpx;
		border-radius: 8rpx;
	}
	
	.item-title {
		font-size: 26rpx;
		color: $u-main-color;
		font-weight: bold;
	}
	
	.item-menu-name {
		font-weight: normal;
		font-size: 24rpx;
		color: $u-main-color;
	}
	
	
	.thumb-box {
		display: flex;
		align-items: center;
		margin-top: 20rpx;
		border-bottom: 1rpx solid #f4f4f4;
		padding-bottom: 10rpx;
		.food_img {
			display: flex;
			align-items: center;
			width: 30%;
		}
		.food_des {
			width: 60%;
			.name {
				font-weight: 700;
			}
			.sale {
				font-size: 28rpx;
				color: #aaa;
				margin: 5rpx 0;
			}
			.price {
				color: #ED2121;
			}
		}
		.add_food {
			width: 20%;
			.plus {
				text-align: center;
				color: #fff;
				line-height: 45rpx;
				width: 50rpx;
				height: 50rpx;
				border-radius: 50%;
				background-color: #F5AC3C;
				font-weight: 700;
			}
		}
	}
	
	.item-menu-image {
		width: 120rpx;
		height: 120rpx;
	}
	
	.cart image{
		width: 120rpx;
		position: absolute;
		bottom: 0;
		left: 5%;
	}
	
	.foot {
		width: 96%;
		position: fixed;
		display: flex;
		bottom: 10px;
		height: 45px;
		// margin: auto;
		border-radius: 10px;
		left: 2%;
		right: 2%;
		line-height: 45px;
		font-weight: 700;
		// padding-right: 20rpx;
		text-align: center;
		font-size: 28rpx;
		
		.cart{
			width: 60%;
			background-color: #F8C271;
			color: #fff;
		}
		.next {
			width: 40%;
			background-color: #ccc;
		}
	}
	.details {
		padding: 30rpx 20rpx;
		.top {
			display: flex;
			justify-content: space-between;
			.choose {
				color: #F5AC3C;
			}
s			.clear {
				// color: #;
				color: #fff;
			}
		}
		.content {
			padding: 30rpx 0;
			.item_choose {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx 0;
			}
		}
		.next {
			text-align: center;
			background-color: #F5AC3C;
			color: #fff;
			padding: 15rpx 0;
			border-radius: 10rpx;
		}
	}
</style>